package com.yungam.influxdb.service;

import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author liuyicheng
 * @description: 计算设备稳定性标准差
 */
@Slf4j
@Service
public class StableInfluxService {

    @Resource
    private InfluxdbCommonService commonService;

    public Double getCurrentData(String ip, Integer collectorId, Long[] range) {
        List<FluxTable> tables = commonService.queryCurrentStdValue(ip, collectorId, range);

        for (FluxTable table : tables) {
            List<FluxRecord> records = table.getRecords();
            for (FluxRecord fluxRecord : records) {
                if (Objects.equals(fluxRecord.getValues().get("ip"), ip)) {
                    return Double.parseDouble(String.valueOf(fluxRecord.getValues().get("_value")));
                }
            }
        }
        return 0d;
    }

    public Double getVoltageData(String ip, Integer[] collectorId, Long[] range) {
        List<FluxTable> tables = commonService.queryVoltageStdValue(ip, collectorId, range);

        for (FluxTable table : tables) {
            List<FluxRecord> records = table.getRecords();
            for (FluxRecord fluxRecord : records) {
                if (Objects.equals(fluxRecord.getValues().get("ip"), ip)) {
                    return Double.parseDouble(String.valueOf(fluxRecord.getValues().get("_value")));
                }
            }
        }
        return 0d;
    }

}
